export default function loadBMap(ak) {
    return new Promise((resolve, reject) => {
        if (window.BMapGL) {
            resolve(window.BMapGL);
            return;
        }

        // 避免重复加载
        if (document.querySelector(`script[src*="api.map.baidu.com"]`)) {
            const checkBMap = setInterval(() => {
                if (window.BMapGL) {
                    clearInterval(checkBMap);
                    resolve(window.BMapGL);
                }
            }, 100);
            return;
        }

        // 动态创建script标签加载地图API（WebGL版）
        const script = document.createElement('script');
        script.type = 'text/javascript';
        // 此处将ak替换为实际密钥
        script.src = `https://api.map.baidu.com/api?v=1.0&type=webgl&ak=BhXLEe1ldliYZqVAn4VC5CiwTqlyGOtT&callback=initBMap`;
        script.onerror = () => {
            reject(new Error('百度地图API加载失败'));
        };
        document.head.appendChild(script);

        // 定义全局回调函数
        window.initBMap = () => {
            resolve(window.BMapGL);
        };
    });
}